import { addSubRoutes, registerRoutesModule } from "@/qiankun-child/addRoutes";
import {
  registerStateModule,
  onGlobalStateChange,
} from "@/qiankun-child/globalState";

const QianKun = {
  install: (Vue, { store, props }) => {
    // 注册路由模块 qiankunRoutes vuex
    registerRoutesModule(store);
    // 为vuex添加states模块,存储qiankun的全局数据
    registerStateModule(store);
    // 添加路由数据
    store.dispatch("qiankunRoutes/SetQiankunRoutes", props.subRoutes);
    // 监听乾坤全局数据变化
    props.onGlobalStateChange((state) => {
      onGlobalStateChange(state, store);
    });
    const $qiankun = {
      token: props.token,
      $router: props.$router,
      $store: props.$store,
      onGlobalStateChange: props.onGlobalStateChange,
      setGlobalState: props.setGlobalState,
      addSubRoutes,
    };
    Vue.prototype.$qiankun = $qiankun;
  },
};
export default QianKun;
